package paul.idtech.lostTraveller.creatures;

import paul.idtech.lostTraveller.R;
import paul.idtech.lostTraveller.model.ItemWeapon;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BaseCreature {

	// variables
	private int health;
	private int maxHealth;
	private int energy;
	private int maxEnergy;
	private int defense;
	public int attackCost;
	private boolean isAlive;
	private ItemWeapon equippedItem;
	private String name;

	public Bitmap creatureImage;
	Context mContext;
	
	private Thread someThread;
	
	

	// constructor
	public BaseCreature(Context context, int maxHealth, int maxEnergy, int defense){
		
		mContext = context;
		
		this.maxHealth = maxHealth;
		health = maxHealth;
		this.maxEnergy=maxEnergy;
		energy = 0;
		this.defense = defense;
		isAlive = true;
		
		//loadBitmap(R.drawable.creature_image);
		
		creatureImage = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.creature_image);
		
		someThread = new Thread(){
			public void run(){
				while(true){
				try{
					
					Thread.sleep(1000);
					regenEnergy();
					
		
					
				}catch(Exception e){
				
			}
			}
			}
				
		};
		someThread.start();
		
	}

	// public void loadBitmap(int ResourceID){

	// creatureImage = BitmapFactory.decodeResource(mContext.getResources(),
	// ResourceID);

	// }

	// methods

	// ///////////////////////Defense
	// returns defense
	public int getDefense() {

		return defense;

	}

	// sets defense
	public void setDefense(int number) {

		defense = number;

	}

	// ///////////////////Health

	// sets health
	public void setHealth(int number) {

		health = number;

	}

	// returns health
	public int getHealth() {

		return health;

	}

	// heals health by number
	public void regenHealthBy(int number) {

		health = health + number;

	}

	// subtracts health by number
	public void damageHealthBy(int number) {

		health = (health + defense) - number;

	}

	// sets maxHealth
	public void setMaxHealth(int number) {

		maxHealth = number;

	}

	// returns maxHealth
	public int getMaxHealth() {

		return maxHealth;

	}

	// checks if creature is alive
	public boolean checkAlive() {

		if (health <= 0) {

			isAlive = false;

		} else {

		}
		return isAlive;
	}

	// //////////////////////////Energy

	// sets energy
	public void setEnergy(int number) {

		energy = number;

	}

	// returns energy
	public int getEnergy() {

		return energy;

	}

	// regenerates energy by number
	public void regenEnergy() {
		if (energy != maxEnergy) {

			energy = energy + 1;
		}else if(energy >=maxEnergy){
			
		}
	}

	// subtracts energy by number
	public void damageEnergyBy(int number) {

		energy = energy - number;

	}

	// sets maxMana
	public void setMaxEnergy(int number) {

		maxEnergy = number;

	}

	// returns maxMana
	public int getMaxEnergy() {

		return maxEnergy;

	}

	// /////////////////////////Misc.

	// changes the equipped item to the entered id
	public void setEquippedItem(int id) {

		equippedItem = new ItemWeapon(id);

	}
	
	//returns equipped item
	public ItemWeapon getEquippedItem(){
		
		return equippedItem;
		
	}

	// sets name
	public void setName(String name) {

		this.name = name;

	}

	// returns name
	public String getName() {

		return name;

	}
	
	public void destroy(){
		
		//Release variables and kill thread
		creatureImage.recycle();
		creatureImage = null;
		
		someThread.interrupt();
		someThread = null;
		
		
	}
	

}
